﻿@model List<VoteSystem.Models.VoteViewModel>
@{
    VoteSystem.Models.VoteConfig config = ViewBag.Config;
 }
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>Matrix Admin</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="~/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/bootstrap-responsive.min.css" />
    <link rel="stylesheet" href="~/css/uniform.css" />
    <link rel="stylesheet" href="~/css/select2.css" />
    <link rel="stylesheet" href="~/css/matrix-style2.css" />
    <link rel="stylesheet" href="~/css/matrix-media.css" />
    <style>
        table {
            color:black;
            font-weight:bold;
        }
    </style>
</head>
<body>

    <div id="content">
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span12">
                    <div class="widget-box">
                        <div class="widget-title">
                            <span class="icon"><i class="icon-th"></i></span>
                            <h5>竞聘职工列表</h5>
                            <h5>当前时间：<span id="currentTime"></span>
                              <span id="info">应当进行第<b id="roundTip" style="color:red">1</b>志愿定聘工作！</span>
                              <span id="disableInfo" style="display:none">当前时间不能进行任何操作</span>
                            </h5>
                        </div>
                        <div class="widget-content nopadding">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>岗位</th>
                                        <th>职工</th>
                                        <th>志愿顺序</th>
                                        <th>已定聘否定聘</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    @foreach (var item in Model)
                                    {
                                        if (item.EmployeeVote1.Count + item.EmployeeVote2.Count + item.EmployeeVote3.Count == 0) 
                                        {
                                            <tr class="round1 position_@item.PositionId">
                                                <td>@item.PositionName</td>
                                                <td colspan="4">此岗位无填报者</td>
                                            </tr>
                                        }
                                        <!--第1志愿职工姓名 -->
                                        foreach (var e in item.EmployeeVote1)
                                        {
                                        <tr class="round1 position_@item.PositionId">
                                            <td>@item.PositionName</td>
                                            <td>@e.EmployeeName</td>
                                            <td>第一志愿</td>
                                            <td id="1_@(item.PositionId)_@(e.EmployeeId)" class="result result_@item.PositionId" data-round="1" data-position="@item.PositionId" data-employee="@e.EmployeeId"></td>
                                            <td>
                                                <button  data-empId="@e.EmployeeId" data-position="@item.PositionId" class="btn-mini btn-employee-@e.EmployeeId" onclick="selectPositionEmployee(@item.PositionId,@e.EmployeeId,true,this,1)">定聘</button>
                                                @*<button class="btn-mini" onclick="selectPositionEmployee(@item.PositionId,@e.EmployeeId,false,this)">取消</button>*@
                                                <a href="javascript:void(0)" onclick="cancel(@item.PositionId,@e.EmployeeId,this,1)">取消</a>
                                            </td>
                                        </tr>
                                        }
                                        <!--第2志愿职工姓名 -->
                                        foreach (var e in item.EmployeeVote2)
                                        {
                                        <tr class="round2 position_@item.PositionId">
                                            <td>@item.PositionName</td>
                                            <td>@e.EmployeeName</td>
                                            <td>第二志愿</td>
                                            <td id="2_@(item.PositionId)_@(e.EmployeeId)"class="result result_@item.PositionId" data-round="2" data-position="@item.PositionId" data-employee="@e.EmployeeId"></td>
                                            <td>
                                                <button data-empId="@e.EmployeeId" data-position="@item.PositionId" class="btn-mini btn-employee-@e.EmployeeId" onclick="selectPositionEmployee(@item.PositionId,@e.EmployeeId,true,this,2)">定聘</button>
                                                <a href="javascript:void(0)" onclick="cancel(@item.PositionId,@e.EmployeeId,this,2)">取消</a>
                                                @*<button class="btn-mini" onclick="selectPositionEmployee(@item.PositionId,@e.EmployeeId,false,this)" >取消</button>*@
                                            </td>
                                        </tr>
                                        }
                                        <!--第3志愿职工姓名 -->
                                        foreach (var e in item.EmployeeVote3)
                                        {
                                        <tr class="round3 position_@item.PositionId">
                                            <td>@item.PositionName</td>
                                            <td>@e.EmployeeName</td>
                                            <td>第三志愿</td>
                                            <td id="3_@(item.PositionId)_@(e.EmployeeId)"class="result result_@item.PositionId" data-round="3" data-position="@item.PositionId" data-employee="@e.EmployeeId"></td>
                                            <td>
                                                <button  data-empId="@e.EmployeeId" data-position="@item.PositionId" class="btn-mini btn-employee-@e.EmployeeId" onclick="selectPositionEmployee(@item.PositionId,@e.EmployeeId,true,this,3)">定聘</button>
                                                <a href="javascript:void(0)" onclick="cancel(@item.PositionId,@e.EmployeeId,this,3)">取消</a>
                                                @*<button class="btn-mini" onclick="selectPositionEmployee(@item.PositionId,@e.EmployeeId,false,this)" >取消</button>*@
                                            </td>
                                        </tr>
                                        }
                                    }

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h3 class="modal-title" id="myModalLabel">
                    提示信息
				</h3>
			</div>
			<div class="modal-body">
                <p></p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal" id="modelClose">关闭
				</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>

    <script src="~/js/jquery.min.js"></script>
    <script src="~/js/jquery.ui.custom.js"></script>
    <script src="~/js/bootstrap.min.js"></script>
    <script src="~/js/jquery.uniform.js"></script>
    <script src="~/js/select2.min.js"></script>
    <script src="~/js/jquery.dataTables.min.js"></script>
    <script src="~/js/matrix.js"></script>
    <script src="~/js/matrix.tables.js"></script>
    <script src="~/js/underscore-min.js"></script>
    <script type="text/javascript">

        var date = new Date(@DateTime.Now.Year,@DateTime.Now.Month-1,
                            @DateTime.Now.Day,@DateTime.Now.Hour,@DateTime.Now.Minute,@DateTime.Now.Second);
        var round1_begin = new Date(@config.StartTime1.Year,@config.StartTime1.Month-1,
                            @config.StartTime1.Day,@config.StartTime1.Hour,@config.StartTime1.Minute,@config.StartTime1.Second);
        var round1_end = new Date(@config.EndTime1.Year,@config.EndTime1.Month-1,
                            @config.EndTime1.Day,@config.EndTime1.Hour,@config.EndTime1.Minute,@config.EndTime1.Second);

        var round2_begin = new Date(@config.StartTime2.Year,@config.StartTime2.Month-1,
                            @config.StartTime2.Day,@config.StartTime2.Hour,@config.StartTime2.Minute,@config.StartTime2.Second);
        var round2_end = new Date(@config.EndTime2.Year,@config.EndTime2.Month-1,
                            @config.EndTime2.Day,@config.EndTime2.Hour,@config.EndTime2.Minute,@config.EndTime2.Second);
        var round3_begin = new Date(@config.StartTime3.Year,@config.StartTime3.Month-1,
                            @config.StartTime3.Day,@config.StartTime3.Hour,@config.StartTime3.Minute,@config.StartTime3.Second);
        var round3_end = new Date(@config.EndTime3.Year,@config.EndTime3.Month-1,
                            @config.EndTime3.Day,@config.EndTime3.Hour,@config.EndTime3.Minute,@config.EndTime3.Second);
        //已定聘人员id
        var selectedEmployeeId = [];


    $(function () {
        showTime();
    });

function showDialog(title,msg) {
	$("#myModal").find(".modal-header h3").html(title);
    $("#myModal").find(".modal-body p").html(msg);
    $("#myModal").modal('show');
}

    function querySelectedEmployee(){
        $.get("/nonesessionajax/queryselectedemployeeid",function(msg){ 
            selectedEmployeeId = msg;
            $(".result").each(function(){$(this).text("");});
            for (var i = 0; i < msg.length; i++) {
                var p = msg[i].PositionId;
                var r = msg[i].Round;
                var e = msg[i].EmployeeId;
                $("#"+r +"_"+p +"_"+e).text("已定聘");
            }
        });
    }
    
    var global_count = 0;
    var timer = null;
    var show1,show2,show3;
    function showTime()
    {
        if( global_count%2 == 0){
            querySelectedEmployee();
        }
        global_count ++;

        var currentTime = $("#currentTime");
        currentTime.text(getDateNow(date));
        date = new Date( date.valueOf() + 1000);
        if( round1_begin.valueOf() < date.valueOf() && date.valueOf() < round1_end.valueOf())
        {
            $("#roundTip").text("1");
            $("#info").show();
            $("#disableInfo").hide();
            $(".round1 .btn-mini").each(function(){
                $(this).removeAttr("disabled");
            });

            $(".round2 .btn-mini").each(function(){
                $(this).attr("disabled","disabled");
            });
            $(".round3 .btn-mini").each(function(){
                $(this).attr("disabled","disabled");
            });
            $(".round2, .round3").hide();
            $(".round1").show();
        }
        else if( round2_begin.valueOf() < date.valueOf() && date.valueOf() < round2_end.valueOf())
        {
            $("#roundTip").text("2");
            $("#info").show();
            $("#disableInfo").hide();
            $(".round1, .round3").hide();
            $(".round2").show();
            $(".round2 .btn-mini").each(function(){
                $(this).removeAttr("disabled");
                for (var i = 0; i < selectedEmployeeId.length; i++) {
                    var p = selectedEmployeeId[i].PositionId;
                    var r = selectedEmployeeId[i].Round;
                    var e = selectedEmployeeId[i].EmployeeId;
                    if(e == $(this).attr("data-empId")&&  r == 1 ){
                        $(this).closest("tr").hide();
                    }
                    if(p == $(this).attr("data-position") && r == 1){ //第一志愿已经选择了该职位
                        $(this).attr("disabled","disabled");
                        $(this).closest("tr").hide();
                    }
                }
            });

            $(".round1 .btn-mini").each(function(){
                $(this).attr("disabled","disabled");
            });
        }
        else if( round3_begin.valueOf() < date.valueOf() && date.valueOf() < round3_end.valueOf())
        {
            $("#roundTip").text("3");
            $("#info").show();
            $("#disableInfo").hide();
            $(".round2, .round1").hide();
            $(".round3").show();
            $(".round3 .btn-mini").each(function(){
                $(this).removeAttr("disabled");
                for (var i = 0; i < selectedEmployeeId.length; i++) {
                    var p = selectedEmployeeId[i].PositionId;
                    var r = selectedEmployeeId[i].Round;
                    var e = selectedEmployeeId[i].EmployeeId;
                    if(e == $(this).attr("data-empId")&& ( r == 1 || r == 2 )){
                        $(this).closest("tr").hide();
                    }
                    if(p == $(this).attr("data-position") && ( r == 1 || r == 2 )){ //第一或者第二志愿已经选了该职位
                        $(this).attr("disabled","disabled");
                        $(this).closest("tr").hide();
                    }
                }
            });

            $(".round1 .btn-mini").each(function(){
                $(this).attr("disabled","disabled");
            });
            $(".round2 .btn-mini").each(function(){
                $(this).attr("disabled","disabled");
            });
        }
        else 
        {
            $("#info").hide();
            $("#disableInfo").show();
            $("table .btn-mini").each(function(){
                $(this).attr("disabled","disabled");
            });
            $(".round1, .round2, .round3").hide();

        }
        setTimeout(showTime,1000);
    }

    //取当前时间，格式为,yyyy-mm-dd hh:mm:ss  
    function getDateNow(d)  
    {  
        var s, month,day, hour,minute,second;  
        s = d.getFullYear() + "-";             //取年份  
        month = d.getMonth() + 1;
        day = d.getDate() ;
        hour =d.getHours() ; 
        minute = d.getMinutes();
        second = d.getSeconds();
        s = s +( month < 10 ? ( "0" +month ) : month )  + "-";//取月份  
        s += ( day < 10 ? ( "0" +day ) : day ) + " ";         //取日期  
        s += ( hour < 10 ? ( "0" + hour) : hour )+ ":";       //取小时  
        s +=( minute < 10 ? ( "0" +minute ) : minute )  + ":";    //取分  
        s +=  second < 10 ? ( "0" +second ) : second ;         //取秒  
    
        return(s);    
   
    }   
    function selectPositionEmployee(positionId, employeeId, flag,obj,round)
    {
        $.post("/account/decide",{positionId:positionId,employeeId:employeeId ,round:round},function(msg){
            if(msg.IsSuccess){
                var tr = $(obj).closest("tr");
                var all_tr = $(".position_"+positionId);
                var allEmployeeBtns = $(".btn-employee-" +employeeId);
                if(flag)
                {
                    var tr = $(obj).closest("tr");
                    var all_tr = $(".position_"+positionId);
                    //all_tr.each(function(){
                    //    $(this).find(".result_"+positionId).text("");
                    //});
                    tr.find(".result_"+positionId).text("已定聘");
                    //allEmployeeBtns.each(function(){
                    //    $(this).attr("disabled","disabled");
                    //});
                }
                else
                {
                    tr.find(".result_"+positionId).text("");
                }
            } else{
                alert("定聘失败,请联系管理员");
            }
        });

        
    }
    
    function cancel(positionId,employeeId,obj,round){
        $.post("/account/undecide",{positionId:positionId,employeeId:employeeId ,round:round},function(msg){
            if(msg.IsSuccess){
                $("#"+round+"_"+positionId+"_"+employeeId).text("");
            } else {
                alert("取消失败,请联系管理员");
            }
        });
    }
    </script>
</body>
</html>
